package me.raih.controller.admin.sales;

import me.raih.pojo.po.Sales;
import me.raih.pojo.vo.ResultInfo;
import me.raih.service.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Raih
 * @Time 2021/3/12  16:56
 */

@Controller
@RequestMapping(value = "/admin")
public class AdminSalesController {

    @Autowired
    SalesService salesService;

    /**
     * 查看售货员列表
     * @return
     */
    @GetMapping(value = "/sales/salesList")
    public String toSalesList(Model model){
        List<Sales> salesList = salesService.findAllSales();
        //查找售货员
        model.addAttribute("SALES_LIST",salesList);
        return "admin/sales/salesList";
    }

    /**
     * 注册售货员
     * @return 售货员
     */
    @RequestMapping(value = "/sales/addSales",method = RequestMethod.PUT)
    @ResponseBody
    public ResultInfo addSales(){
        ResultInfo resultInfo = new ResultInfo();
        boolean flag = salesService.addSales();
        resultInfo.setFlag(flag);
        return resultInfo;
    }

    /**
     * 删除售货员啊
     * @param username 售货员用户名
     * @return 禁用了
     */
    @RequestMapping(value = "/sales/deleteSales",method = RequestMethod.DELETE)
    @ResponseBody
    public ResultInfo updateLevel(@RequestParam("username") String username){
        ResultInfo resultInfo = new ResultInfo();
        boolean flag = salesService.deleteSalesByUsername(username);
        resultInfo.setFlag(flag);
        if(flag){
            resultInfo.setMessage("注销成功！");
        }else {
            resultInfo.setMessage("注销失败！");
        }
        return resultInfo;
    }


}
